﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MinhTuan;
using System.Web.DataAccess;
using System.Web.UI.WebControls;

namespace Products
{
   public class SelectedList
   {
       #region Properties

       private List<SelectedProduct> _Items ;
       
       public List<SelectedProduct> Items
       {
           get { return _Items; }
       }

       #endregion

       #region Constructor
       public SelectedList()
           {
                _Items = new List<SelectedProduct>();
           }
       #endregion

       #region Public Properties

       public int Quantity
       {
           get
           {
               int tempResult = 0;
               foreach (SelectedProduct mySelected in _Items)
               {
                   tempResult += mySelected.Quantity;
               }
               return tempResult;
           }
       }
 
       public Decimal Total
       {
           get
           {
               Decimal tempResult = 0;
               foreach (SelectedProduct mySelectedProduct in _Items)
               {
                   tempResult += mySelectedProduct.SubTotal;
               }
               return tempResult;
           }
       }

       public int Count
       {
           get { return _Items.Count; }
       }

       #endregion

       #region Method

       public void Add(SelectedProduct mySelectedProduct)
       {
           foreach (SelectedProduct item in _Items)
           {
               if (mySelectedProduct.Id == item.Id)
               {
                   item.Quantity += 1;
                   return;
               }
           }
           _Items.Add(mySelectedProduct);
       }

       public void Add(Int32 Id, Int32 Quantity)
       {
           foreach (SelectedProduct item in _Items)
           {
               if (Id == item.Id)
               {
                   item.Quantity += Quantity;
                   break;
               }
           }
           _Items.Add(new SelectedProduct(Id, Quantity));
       }

       public static SelectedList GetSelectedItems(String SelectItems)
       {
           SelectedList  SelectedItems = new SelectedList();
           //try
           //{
               if (SelectItems.Length > 0)
               {
                   SelectItems = SelectItems.Remove(0, 1);
                   String[] CurrSelect = SelectItems.Split('-');
                   String[] ItemInfo;
                   SelectedProduct myItem;
                   for (Int16 index = 0; index < CurrSelect.Length; index++)
                   {
                       ItemInfo = CurrSelect[index].Split(',');
                       myItem = new SelectedProduct(Convert.ToInt32(ItemInfo[0]), Convert.ToInt32(ItemInfo[1]));
                       SelectedItems.Add(myItem);
                   }
                   Access.InitAccess();
                   SqlDataReader myReader;
                   for (Int16 index = 0; index < SelectedItems.Count; index++)
                   {
                       Access.mySqlCommand.Parameters.Clear();
                       Access.mySqlCommand.CommandText = "Sp_Products_GetSelectedItemDetail";
                       Access.mySqlCommand.Parameters.Add("@Item_Id", SqlDbType.Int).Value = SelectedItems.Items[index].Id;
         
                       myReader = Access.mySqlCommand.ExecuteReader();
                       if (myReader.Read())
                       {
                           SelectedItems.Items[index].Image = myReader["Products_Image"].ToString();
                           SelectedItems.Items[index].Name = myReader["Products_Name"].ToString();
                           SelectedItems.Items[index].Price = Convert.ToDecimal(myReader["Products_Price"]);
                           SelectedItems.Items[index].Unit = myReader["Products_Unit"].ToString();
                           SelectedItems.Items[index].URL = myReader["Products_Title"].ToString();
                       }
                       myReader.Close();
                   }
               }
               return SelectedItems;
           //}
           //catch (Exception)
           //{
           //    return SelectedItems;
           //}
       }

       //public static SelectedList GetSelectedItems(ListView Container)
       //{
       //    SelectedList SelectedItems = new SelectedList();
       //    try
       //    {
       //        String[] cartinfo;
       //        SelectedProduct mySelectedProduct = new SelectedProduct();
       //        foreach (ListViewItem myItem in Container.Items)
       //        {

       //            if (myItem.ItemType == ListViewItemType.DataItem)
       //            {
       //                HiddenField hdinfo = (HiddenField)myItem.FindControl("hdinfo");
       //                TextBox inputquantity = (TextBox)myItem.FindControl("inputquantity");
       //                if (hdinfo != null && inputquantity != null)
       //                {
       //                    cartinfo = hdinfo.Value.ToString().Split(',');
       //                    mySelectedProduct = new SelectedProduct(Convert.ToInt32(cartinfo[0]), cartinfo[1], cartinfo[3], Convert.ToInt32(inputquantity.Text), Convert.ToDecimal(cartinfo[2]));
       //                    SelectedItems.Add(mySelectedProduct);
       //                }
       //            }
       //        }
       //        return SelectedItems;
       //    }
       //    catch (Exception)
       //    {
       //        return SelectedItems;
       //    }
       //}
       public static DataTable GetSelectedItem(int Order_Id)
       {
           DataTable tbl = new DataTable();
           try
           {
               Access.InitAccess();
               Access.mySqlCommand.CommandText = "Sp_Orders_Details_GetItem";
               Access.mySqlCommand.Parameters.Add("@Orders_Id", SqlDbType.Int).Value = Order_Id;
               DataSet ds = new DataSet();
               SqlDataAdapter da = new SqlDataAdapter(Access.mySqlCommand);
               da.Fill(ds);
               tbl = ds.Tables[0];
               Access.ClosedAccess();
               return tbl;
           }
           catch {
               Access.ClosedAccess();
               return tbl;
           }
       }
       public static String UpdateShoppingcart(SelectedList myShoppingCart)
       {
           String Value = "";
           foreach (SelectedProduct myItem in myShoppingCart.Items)
           {
               Value += "-" + myItem.Id.ToString() + "," + myItem.Quantity.ToString();
           }
           return Value;
       }
       public static String UpdateOrdercart(SelectedList myShoppingCart)
       {
           String Value = "";
           foreach (SelectedProduct myItem in myShoppingCart.Items)
           {
               Value += "-" + myItem.Id.ToString() +","+myItem.Name.ToString()+ "," + myItem.Quantity.ToString()+","+myItem.Price.ToString();
           }
           return Value;
       }

       public static String DeleteSelectedItem(String Item, String SelectedItems)
       {
            SelectedItems = SelectedItems.Replace("-" + Item ,"");
            SelectedItems = SelectedItems.Replace(Item + "-", "");
            SelectedItems = SelectedItems.Replace(Item ,"");
            return SelectedItems;
       }
      
      
     
       #endregion
   }
}
